package com.gpframework.module.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.core.domain.entity.SysDept;
import com.gpframework.common.core.domain.entity.SysRole;
import com.gpframework.common.core.domain.entity.SysUser;
import com.gpframework.common.core.domain.model.LoginBody;
import com.gpframework.common.enums.CommonWxUserEnum;
import com.gpframework.common.enums.UserFollowEnum;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.exception.BaseException;
import com.gpframework.module.module.domain.req.BrandStoreReq;
import com.gpframework.module.module.domain.vo.BrandStoreVo;
import com.gpframework.module.module.service.IBrandStoreService;
import com.gpframework.module.pay.utils.joinpay.sdk.utitls.StringUtil;
import com.gpframework.module.security.utils.SecurityUtils;
import com.gpframework.module.system.domain.SysUserDTO;
import com.gpframework.module.system.mapper.SysDeptMapper;
import com.gpframework.module.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;

/**
 * 登录校验方法
 *
 * @Author gpframework
 */
@Component
public class SysLoginService {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IBrandStoreService brandStoreService;


    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 登录验证
     *
     * @param loginBody
     * @return 结果
     */
    public SysUserDTO login(LoginBody loginBody) {
        SysUser sysUser = sysUserService.selectUserByUserName(loginBody.getUsername(), loginBody.getBrandId());
        if (StringUtils.isEmpty(sysUser) || !sysUser.getBrandId().equals(loginBody.getBrandId())) {
            throw new BaseException("账号/密码错误");
        }
        boolean success = SecurityUtils.matchesPassword(loginBody.getPassword(), sysUser.getPassword());
        if (!success) {
            throw new BaseException("账号/密码错误");
        }
        BrandStoreReq brandStoreReq = new BrandStoreReq();
        brandStoreReq.setBrandId(loginBody.getBrandId());
        if (!StringUtils.isEmpty(sysUser.getIsAdmin()) && !sysUser.whetherAdmin() && !StringUtils.isEmpty(sysUser.getWxUserId())) {
            brandStoreReq.setUserId(sysUser.getWxUserId());
        }
        SysUserDTO userDTO = Convert.convert(new TypeReference<SysUserDTO>() {
        }, sysUser);
        List<SysDept> deptList = sysDeptMapper.findByBrandUserId(userDTO.getBrandUserId());
        if (StringUtil.isNotEmpty(deptList) && deptList.size() > 0) {
            userDTO.setDeptList(deptList);
        }
        List<SysRole> sysRoleList = sysRoleMapper.selectRolePermissionByUserId(sysUser.getBrandUserId());
        if (!CollectionUtils.isEmpty(sysRoleList)) {
            String minDataScope = sysRoleList.stream().map(SysRole::getDataScope).min(Comparator.naturalOrder()).orElse(null);
            userDTO.setDataScope(minDataScope);
        }
        List<BrandStoreVo> storeVoList = brandStoreService.findByBrandId(brandStoreReq);
        userDTO.setStoreVoList(storeVoList);
        brandStoreReq.setStoreId(sysUser.getStoreId());
        brandStoreReq.setUserName(sysUser.getUserName());
        BrandStoreVo storeVo = brandStoreService.get(brandStoreReq);
        if (!StringUtils.isEmpty(storeVo)) {
            userDTO.setStoreAdmin(Integer.parseInt(YesNoEnum.NO.getKey()));
        }
        userDTO.setLoginToken(tokenService.createToken(sysUser));
//        //潜在客户统计
//        UserFollowReq userFollowReq = new UserFollowReq();
//        userFollowReq.setSalesmanId(userDTO.getWxUserId());
//        userFollowReq.setMemberType(CommonWxUserEnum.POTENTIAL_USER.getKey());
//        userFollowReq.setFollowStatus(UserFollowEnum.FOLLOWING.getKey());
//        Integer customerNum = iUserFollowService.getUserCount(userFollowReq);
//        userDTO.setCustomerNum(customerNum);
//        //会员统计
//        userFollowReq.setMemberType(CommonWxUserEnum.FORMAL_MEMBER.getKey());
//        Integer memberNum = iUserFollowService.getUserCount(userFollowReq);
//        userDTO.setMemberNum(memberNum);
        return userDTO;
    }
}
